import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
import { AssetType, getPhotos, GroupType } from "@react-native-community/cameraroll";
import { PermissionsAndroid } from "react-native";

export interface PostListState {
    images: Array<any>, // 存放照片数据
    endCursor: string, 
    hasNextPage: true // 是否还有下一张照片
    loading: boolean; // 请求数据中
    error: string | null; // 是否错误
}

export type QueryParams = {
    assetType: AssetType;
    groupTypes?:GroupType;
    groupName?:string;
    reset?:boolean;
}



const initialState: PostListState = {
    images: [],
    hasNextPage: true,
    loading: false,
    endCursor:"",
    error: null,
};


export const getMediaList = createAsyncThunk<any, QueryParams>(
    "getMediaList",
    async (params: QueryParams, { rejectWithValue, getState }) => {
        const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
        const hasPermission = await PermissionsAndroid.check(permission);
        if(!hasPermission){
            const status = await PermissionsAndroid.request(permission);
            if(status !== "granted"){
                return rejectWithValue({
                    code:-1,
                    message:"未授权"
                });
            }
        }
        const { mediaList } = getState() as any;
        const { hasNextPage,endCursor } = mediaList;
        if(!hasNextPage){
            return {
                edges:[],
                page_info:{
                    end_cursor:endCursor,
                    has_next_page:false
                }
            };
        }
        const first = 100 + Number(endCursor || 0);
        try {
            const result = await getPhotos({
                ...params,
                first:first,
                include: ["playableDuration"],
                after: endCursor
            });
            if (params.reset) {
                // @ts-ignore
                result.reset = true;
            }
            return result;
        } catch (error) {
            return rejectWithValue(error);
        }
    }
);

export const MediaListSlice = createSlice({
    name: "mediaList",
    initialState,
    reducers: {
    },
    extraReducers: {
        [getMediaList.pending.type]: (state) => {
            state.loading = true;
        },
        [getMediaList.fulfilled.type]: (state, action) => {
            const { edges, page_info, reset = false  } = action.payload;
            if (reset) {
                state.images = edges;
            } else {
                // state.images = state.images.concat(edges);
                state.images = edges;
            }
            console.log(page_info);
            state.loading = false;
            state.endCursor = page_info.end_cursor;
            state.hasNextPage = page_info.has_next_page;
        },
        [getMediaList.rejected.type]: (state, action: PayloadAction<string | null>) => {
            state.loading = false;
            state.error = action.payload;
        }
    }
});



